/*
 * @Date: 2024-04-01 09:33:35
 * @LastEditors: bingkui.wu
 * @LastEditTime: 2024-04-02 11:21:58
 * @Description: 获取用户信息接口
 */
import { REQUEST } from "#type/request";
import Response from "@common/Response";
import { User } from "@models/User";
import verifyToken from "@/helper/verifyToken";

const root: REQUEST.BASIC = {
  auth: false,
  method: "post",
  request: async (ctx: any) => {
    try {
      const user = await verifyToken(ctx.header.authorization);
      const { _id } = user as any;
      let obj = await User.findOne({ _id }).lean().exec();
      if (obj) {
        return Response.Success({
          userName: obj.userName,
          nickName: obj.nickName,
          starCount: obj.starCount || 0,
          correctWords: obj.correctWords || [],
          incorrectWords: obj.incorrectWords || [],
          joinDays: Math.ceil(
            (new Date().getTime() - obj.createTime) / (1000 * 60 * 60 * 24)
          ),
        });
      } else {
        return Response.Error(10001, `没有找到用户:${_id}`);
      }
    } catch (err) {
      console.log(err);
      return err;
    }
  },
};

export default root;
